#
# Copyright 2010 Steve Leach
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#

from google.appengine.ext import db
from google.appengine.api import users

import logging

from eveoperation import Pilot, Corp
from utils import get_role_list
from google.appengine.api import memcache

class EveData():
	"""
	Represents data about the user's Eve Online session.
	Based on Eve IGB (in-game-browser) headers.
	"""
	
	trusted = False
	char_id = '0'
	char_name = '{unknown}'
	char_id = '0'
	corp_name = '{unknown}'
	corp_id = '0'
	system_name = '{unknown}'
	debug = 'none'
	count = -3
	stylesheet = 'ingame'
	google_account = None
	
	def __init__(self, headers):
		self.debug = 'init'
		self.count = len(headers)

	  	if 'Eve-Trusted' in headers:
	  		self.trusted = headers['Eve-Trusted'] == 'Yes'

	  	if 'EVE_CHARID' in headers:
	  		self.char_id = headers['EVE_CHARID']

	  	if 'Eve-Charid' in headers:
	  		self.char_id = headers['Eve-Charid']

	  	if 'Eve-Corpid' in headers:
			self.debug = 'corpid'
	  		self.corp_id = headers['Eve-Corpid']

		if 'Eve-Solarsystemname' in headers:
	  		self.system_name = headers['Eve-Solarsystemname']
	  			  		
	  	if 'EVE_CHARNAME' in headers:
	  		self.char_name = headers['EVE_CHARNAME']
	  		self.debug = 'B'
	  			  			
	  	if 'EVE_SOLARSYSTEMNAME' in headers:
	  		self.system_name = headers['EVE_SOLARSYSTEMNAME']

	  	if 'EVE_CORPNAME' in headers:
	  		self.corp_name = headers['EVE_CORPNAME']
	  		
	  	if 'Eve-Corpname' in headers:
	  		self.corp_name = headers['Eve-Corpname']	

		self.corp_roles = []

	  	if 'Eve-Corprole' in headers:
	  		self.corp_roles = get_role_list(headers['Eve-Corprole'])

		self.is_director = ('Director' in self.corp_roles)

def get_pilot(char_name):
	if not char_name:
		return None

	pilot = memcache.get("pilot " + char_name)
	if not pilot:
		pilot = Pilot.gql('WHERE char_name = :1', char_name).get()
		if pilot:
			memcache.set("pilot " + char_name, pilot, 120)

	return pilot

def google_to_pilot(google_id):
	"""
	Lookup the pilot name associated with a particular google ID.
	"""
	if not google_id:
		return None

	char_name = memcache.get("google " + google_id)
	if not char_name:
		pilot = Pilot.gql('WHERE google_account = :1', google_id).get()
		if pilot:
			char_name = pilot.char_name
			memcache.set("google " + google_id, char_name, 14400)
			memcache.set("pilot " + char_name, pilot, 120)
			logging.info("Mapping google ID " + str(google_id) + " to pilot " + str(char_name))

	return char_name

def get_corp_name(corp_id):
	"""
	Convert corp ID to corp name.
	"""
	corp = Corp.gql('WHERE corp_id = :1', corp_id).get()
	if corp:
		return corp.corp_name

def get_eve_data(headers):
	# See what we can extract from Eve IGB headers
	eve_data = EveData(headers)

	# Then look for a google account for the current user
	if users.get_current_user():
		eve_data.google_account = users.get_current_user().email()

	if eve_data.char_name == '{unknown}':
		# We don't have IGB headers
		if eve_data.google_account:
			# But we do have a mapping from google ID to pilot
			eve_data.char_name = google_to_pilot(eve_data.google_account)

	if eve_data.google_account:
		if eve_data.char_name == '{unknown}':
			# We have a google account but not a char name
			logging.info('Could not map google account ' + eve_data.google_account)

	pilot = get_pilot(eve_data.char_name)

	if pilot:
		eve_data.stylesheet = pilot.stylesheet
		if pilot.corp_id:
			eve_data.corp_id = pilot.corp_id
			eve_data.corp_name = get_corp_name(pilot.corp_id)
	else:
		eve_data.stylesheet = 'ingame'

	return eve_data

#
# END
#
